package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.awt.print.Book;
import java.util.List;

@RestController
@RequestMapping("/user/addressBook")
@Api(tags = "C端-地址栏接口")
@Slf4j
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    /**
     * 新增收货地址
     *
     * @param addressBook
     * @return
     */
    @PostMapping
    @ApiOperation("新增收货地址")
    public Result add(@RequestBody AddressBook addressBook) {
        log.info("新增收货地址:{}", addressBook);
        addressBookService.addAddress(addressBook);
        return Result.success();
    }

    /**
     * 查询当前用户的所有地址
     *
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("查询当前用户的所有地址")
    public Result<List<AddressBook>> list() {
        log.info("查询当前用户的所有收货地址");
        List<AddressBook> addressBookList = addressBookService.list();
        return Result.success(addressBookList);
    }

    /**
     * 查询当前用户的默认地址
     *
     * @return
     */
    @GetMapping("/default")
    @ApiOperation("查询当前用户的默认地址")
    public Result<AddressBook> queryByDefault() {
        AddressBook addressBook = addressBookService.queryByDefault();
        if (addressBook == null) {
            return Result.error("没有查询到默认地址");
        }
        return Result.success(addressBook);

    }

    /**
     * 设置默认地址
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    @ApiOperation("设置默认地址")
    public Result setDefault(@RequestBody AddressBook addressBook) {
        log.info("设置默认地址");
        addressBookService.setDefault(addressBook);
        return Result.success();
    }

    /**
     * 根据id查询地址
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询地址")
    public Result<AddressBook> queryById(@PathVariable Long id){
        AddressBook addressBook =addressBookService.queryById(id);
        return Result.success(addressBook);
    }


    /**
     * 根据id修改地址
     * @param id
     * @return
     */
    @PutMapping
    @ApiOperation("根据id修改地址")
    public Result updateById(@RequestBody AddressBook addressBook){
        addressBookService.updateById(addressBook);
        return Result.success();
    }
    @DeleteMapping
    @ApiOperation("根据id删除地址")
    public Result delById(Long id){
        addressBookService.delById(id);
        return Result.success();
    }
}
